package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCDemo9 {
    public static void main(String[] args) {
        System.out.println("欢迎注册");
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String userName = scan.nextLine();
        System.out.println("请输入用户密码：");
        String passWord = scan.nextLine();
        System.out.println("请输入昵称：");
        String nickName = scan.nextLine();
        System.out.println("请输入年龄：");
        int age = scan.nextInt();
        try (
                Connection connection = DBUtil.getConnection();
        ) {
            String sql = "INSERT INTO user(username,password,nickname,age)" +
                    "VALUES(?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, userName);
            ps.setString(2, passWord);
            ps.setString(3, nickName);
            ps.setInt(4, age);
            int num = ps.executeUpdate();
            System.out.println(num > 0 ? "注册成功" : "注册失败");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
